package cn.ytr.project.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;

import cn.ytr.project.domain.Department;
import cn.ytr.project.domain.Employee;
import cn.ytr.project.service.IDepartmentService;
import cn.ytr.project.service.IEmployeeService;
import cn.ytr.project.service.impl.DepartmentServiceImpl;
import cn.ytr.project.service.impl.EmployeeServiceImpl;
import cn.ytr.project.util.StringUtil;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet{

	private static final long serialVersionUID = -7204134657884404273L;
	private IEmployeeService employeeService = new EmployeeServiceImpl();
	private IDepartmentService departmentService = new DepartmentServiceImpl();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String cmd = request.getParameter("cmd");
		if ("delete".equals(cmd)) {
			delete(request, response);
		} else if ("input".equals(cmd)) {
			input(request, response);
		} else if ("insertOrUpdate".equals(cmd)) {
			insertOrUpdate(request, response);
		} else if ("employeeList".equals(cmd)) {
			employeeList(request, response);
		} else {
			list(request, response);
		}
	}
	
	/**
	 * 显示 某部门下的所有员工 列表页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void employeeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String departmentId = request.getParameter("id");
		if (StringUtil.hasLength(departmentId)) {
			Department department = departmentService.selectOneAndEmployeesById(Long.valueOf(departmentId));
			request.setAttribute("department", department);
		}
		request.getRequestDispatcher("/WEB-INF/views/departmentEmployeesList.jsp").forward(request, response);
	}
	
	/**
	 * input 页面提交时处理的方法，添加 或者 编辑部门
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void insertOrUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String[] employeeIds = request.getParameterValues("employeeIds");
		
		Department department = new Department();
		department.setName(name);
		
		
		//判断是 编辑部门 还是 添加部门
		if (StringUtil.hasLength(id)) {
			department.setId(Long.valueOf(id));
			//先把该部门ID下的员工的部门ID清空，再根据多选框勾选来给员工的部门ID赋值
			String[] originalEmployeeIds = employeeService.getIdsByDeptId(Long.valueOf(id));
			System.out.println(originalEmployeeIds);
			if (originalEmployeeIds.length > 0) {
				employeeService.updateDeptIdInIds(null, originalEmployeeIds);
			}
			departmentService.update(department);
		} else {
			departmentService.insert(department);
		}
		
		//如果是新增部门的话，部门id需要sql那边自动获取生成的
		//根据多选框勾选来给员工的部门ID赋值
		if (employeeIds != null && employeeIds.length > 0) {
			if (StringUtil.hasLength(id)) {
				employeeService.updateDeptIdInIds(Long.valueOf(id), employeeIds);
			} else {
				employeeService.updateDeptIdInIds(department.getId(), employeeIds);
			}
		}
		
		response.sendRedirect("/department");
	}
	
	/**
	 * 显示 input 页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void input(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		//判断是编辑部门还是添加部门
		if (StringUtil.hasLength(id)) {
			Department department = departmentService.selectOneAndEmployeesById(Long.valueOf(id));
			List<Employee> employees = department.getEmployees();
			request.setAttribute("department", department);
			request.setAttribute("employees", employees);
		} else {
			
		}
		
		//没有部门的员工
		List<Employee> nullDeptEmps = employeeService.selectForListByDeptIdIsNull();
		request.setAttribute("nullDeptEmps", nullDeptEmps);
		request.getRequestDispatcher("/WEB-INF/views/departmentInput.jsp").forward(request, response);
	}
	
	@Test
	public void test() throws Exception {
		String[] originalEmployeeIds = employeeService.getIdsByDeptId(3L);
		for (String string : originalEmployeeIds) {
			System.out.println("---" + string);
		}
	}
	
	/**
	 * 显示部门列表页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Department> list = departmentService.selectAllForList();
		request.setAttribute("departments", list);
		request.getRequestDispatcher("/WEB-INF/views/departmentList.jsp").forward(request, response);
	}
	
	/**
	 * 删除部门 的处理方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//删除部门的时候，要处理好该部门下的员工，把员工的 deptId 设置为空
		String departmentId = request.getParameter("id");
		if (StringUtil.hasLength(departmentId)) {
			String[] empIdsByDeptId = employeeService.getIdsByDeptId(Long.valueOf(departmentId));
			employeeService.updateDeptIdInIds(null, empIdsByDeptId);
			departmentService.delete(Long.valueOf(departmentId));
		}
		response.sendRedirect("/department");
	}
	
}
